import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:scrollview_observer/scrollview_observer.dart';
import 'package:sports_new/controller/home/comment_view_logic.dart';
import 'package:sports_new/controller/home/news_detail_logic.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/resources/styles.dart';
import 'package:sports_new/utils/local_read_history.dart';
import 'package:sports_new/utils/widget_utils.dart';
import 'package:umeng_apm_sdk/umeng_apm_sdk.dart';
import 'package:sports_new/view/no_privacy/zixun_detial_view.dart';

import '../../widgets/base/qkd_text.dart';

class ZixunDetailPage extends StatefulWidget {
  const ZixunDetailPage({super.key});

  @override
  State<ZixunDetailPage> createState() => _ZixunDetailPageState();
}

class _ZixunDetailPageState extends State<ZixunDetailPage> {
  int id = Get.arguments.toInt() ?? 0;
  late final NewsDetailLogic logic;
  late final CommentViewLogic commentLogic;

  late int? classId = int.tryParse(Get.parameters['classId'] ?? "");
  EasyRefreshController refreshC =
      EasyRefreshController(controlFinishLoad: true);

  // late final commentsController = NewsCommentsController(id ?? 0);
  final scrollC = ApmScrollController();
  bool showTop = false;

  late ListObserverController observerController;

  @override
  void initState() {
    super.initState();
    // commentsController.fetchCommentds();
    // AppNewsFloat2Widget.delNews(id);
    logic = Get.put(NewsDetailLogic(), tag: '${Get.arguments}');
    commentLogic = Get.put(CommentViewLogic(), tag: '${Get.arguments}');
    LocalReadHistory.readNews("${logic.newsId}");
    observerController = ListObserverController(controller: scrollC);
    scrollC.addListener(() {
      if (scrollC.offset > 60 && showTop == false) {
        showTop = true;
        update();
      } else if (scrollC.offset < 60 && showTop == true) {
        showTop = false;
        update();
      }
    });
  }

  @override
  void dispose() {
    scrollC.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    bool loading = logic.data.toString().isEmpty;

    return loading
        ? const CircularProgressIndicator()
        : GetBuilder<NewsDetailLogic>(
            tag: '${id}',
            builder: (_) => Scaffold(
              resizeToAvoidBottomInset: false,
              backgroundColor: Colours.white,
              appBar: Styles.appBar(
                titleSpacing: 0,
                // toolbarHeight: kToolbarHeight,
                // title: Image.asset(Utils.getImgPath("qxb_title.png")),
                title: QkdText('内容详情'),
              ),
              body: logic.data == null
                  ? Container()
                  : Column(
                      children: [
                        Expanded(
                          child: ListViewObserver(
                            controller: observerController,
                            child: ListView(controller: scrollC, children: [
                              Column(
                                children: [ZixunDetailView(data: logic.data)],
                              ),
                            ]),
                          ),
                        ),
                      ],
                    ),
            ),
          );
  }
}
